package com.chujian.Activities;

import com.chujian.Global.Global;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.Utils.Util;
import com.chujian.model.Food;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AddFoodActivity extends Activity implements OnClickListener {
	private EditText mEtFoodName;
	private EditText mEtFoodPrice;
	private EditText mEtUnit;
	private CheckBox mCbQuehuo;
	private ImageView mIvAddImage;
	private Button mBtnSubmit;
	private ImageButton mIbtnBack;
	private String foodName;
	private String foodPrice;
	private String foodUnit;
	private String foodCategary;
	private Handler mHandler;
	private Food modifyFood;
	private ImageButton mIbBack;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_food);
		initView();
		mHandler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				String str=(String) msg.obj;
				 if(str.equals("addFailed")){
					Toast.makeText(AddFoodActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
				}else if(str.equals("updateFailed")){
					Toast.makeText(AddFoodActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
				}else {
					Global.categaryNewAdd=foodCategary;
					Food food=JsonUtil.FoodFromJson(str);
					Bundle bundle=new Bundle();
					Intent intent =new Intent();
					bundle.putSerializable("food", food);
					intent.putExtras(bundle);
					if(modifyFood==null){
						Toast.makeText(AddFoodActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
						setResult(1001, intent);
					}else{
						Toast.makeText(AddFoodActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
						setResult(1002, intent);
					}
					AddFoodActivity.this.finish();
				}
			}
		};

	}

	private void initView() {
		// TODO Auto-generated method stub
		mEtFoodName = (EditText) findViewById(R.id.et_addfood_name);
		mEtFoodPrice = (EditText) findViewById(R.id.et_addfood_price);
		mEtUnit = (EditText) findViewById(R.id.et_addfood_unit);
		mCbQuehuo = (CheckBox) findViewById(R.id.cb_addfood_quehuo);
		mIvAddImage = (ImageView) findViewById(R.id.iv_addfood_image);
		mBtnSubmit = (Button) findViewById(R.id.btn_addfood_submit);
		mIbBack=(ImageButton) findViewById(R.id.ib_header_back);
		mIbBack.setOnClickListener(this);
		mBtnSubmit.setOnClickListener(this);
		mIvAddImage.setOnClickListener(this);
		
		Intent intent=getIntent();
		foodCategary=intent.getStringExtra("foodCategary");
		modifyFood=(Food) intent.getSerializableExtra("food");
		if(modifyFood!=null){
			mEtFoodName.setText(modifyFood.getFoodName());
			mEtFoodPrice.setText(""+modifyFood.getFoodPrice());
			mEtUnit.setText(modifyFood.getFoodUnit());
			if(modifyFood.getFoodStatus().equals("1")){
				mCbQuehuo.setChecked(true);
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.iv_addfood_image:
			// 添加图片

			break;
		case R.id.btn_addfood_submit:
			// 数据获取
			foodName = mEtFoodName.getText().toString();
			foodPrice = mEtFoodPrice.getText().toString();
			foodUnit = mEtUnit.getText().toString();
			// 提交数据
			if (foodName.equals("") || foodUnit.equals("")
					|| foodPrice.equals("")) {
				Toast.makeText(AddFoodActivity.this, "请将信息填写完整",
						Toast.LENGTH_SHORT).show();
			} else {

				Food food = new Food();

				food.setFoodName(foodName);
				food.setFoodPrice(Float.parseFloat(foodPrice));
				food.setFoodUnit(foodUnit);
				food.setFoodCategary(foodCategary);
				food.setFoodUrl("empty");
				if(mCbQuehuo.isChecked()){
					food.setFoodStatus("1");
				}else{
					food.setFoodStatus("0");
				}
				String url="";
				if(modifyFood==null){
					url = Constan.BASE_URL+"AddFoodServlet";
				}else{
					food.setFoodId(modifyFood.getFoodId());
					url = Constan.BASE_URL+"ModifyFood";
				}
				Gson gson = new Gson();
				String jsonString = gson.toJson(food);
				new HttpClientPostThread(url, mHandler, jsonString).start();;
			}
			break;
		case R.id.ib_header_back:
			AddFoodActivity.this.finish();
			break;
		default:
			break;
		}

	}
	
	
}
